<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
</style>

<template>
  <div>
    <Row :gutter="10">
      <Card>
        <div class="table-head">
          <div class="left">
            <Select v-model="hold_id" :transfer="true" @on-change="onChangeHoldSelect">
              <Option v-for="item in holdArr" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </div>
          <div class="left">
            <Select v-model="type_id" :transfer="true" @on-change="onChangeTypeSelect">
              <Option v-for="item in typeArr" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </div>
          <Button type="primary" class="Button" @click="remove()">删除</Button>
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <Row class="margin-top-10">
          <Table stripe border :columns="columns" :data="data" @on-row-click="onRowData"
                 @on-selection-change="selectionChange"></Table>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      <Modal :width="600" title="详情"
             v-model="showCurrentTableData"
             ok-text="确定"
             cancel-text="取消"
             @on-ok=""
             @on-cancel=""
             class-name="vertical-center-modal"
             class="Modal-Detail-div">
        <Form label-position="top">
          <FormItem label="物品名">
            <div class="dataDetail-div" v-text="dataDetail.asset_type ? dataDetail.asset_type.title : ''"></div>
          </FormItem>
          <FormItem label="数量">
            <div class="dataDetail-div" v-text="dataDetail.asset_count"></div>
          </FormItem>
          <FormItem label="物品编号">
            <div class="dataDetail-div">
              <div v-for="el in dataDetail.asset_number" v-text="el"></div>
            </div>
          </FormItem>
          <FormItem label="申请时间">
            <div class="dataDetail-div" v-text="dataDetail.create_time"></div>
          </FormItem>
          <FormItem label="报修原因">
            <div class="dataDetail-div" v-text="dataDetail.title"></div>
          </FormItem>
          <FormItem label="报修说明">
            <div class="dataDetail-div" v-text="dataDetail.description"></div>
          </FormItem>
          <FormItem label="状态">
            <div class="dataDetail-div">
              <span v-if="dataDetail.status == 'checking'">待审批</span>
              <span v-if="dataDetail.status == 'passed'">已通过</span>
              <span v-if="dataDetail.status == 'rejected'">已驳回</span>
            </div>
          </FormItem>
        </Form>
      </Modal>
      <!--编辑-->
      <Modal v-model="addCurrentTableData" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">资产维护</h3>
        <Form label-position="top">
          <FormItem label="物品名">
            <Select v-model="addData.type_id" :transfer="true" style="width:100%;">
              <Option v-for="item in addData.typeArr" :value="item.id" :key="item.id">{{ item.title }}</Option>
            </Select>
          </FormItem>
          <FormItem label="编号">
            <Input v-model="addData.asset_number" placeholder="编号"></Input>
          </FormItem>
          <FormItem label="备注">
            <Input v-model="addData.remark" type="textarea" class="textarea_resize"
                   :autosize="{minRows: 3}" placeholder="备注"></Input>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="hideAddModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
        </div>
      </Modal>
    </Row>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Modal from '../../libs/mod.Modal';
  export default {
    components: {},
    data () {
      return {
        total: 0,
        page: 1,
        size: 10,
        id: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        saveLoading: false,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'type',
            title: '物品名',
            render: (h, params) => {
              var title = params.row.type.title;
              return h('div', [
                h('span', title)
              ]);
            }
          },
          {
            key: 'asset_number',
            title: '编号',
          },
          {
            key: 'user_name',
            title: '持有人',
          },
          {
            key: 'remark',
            title: '备注',
          },
          {
            title: 'Action',
            key: 'action',
            width: 100,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: e => {
                      e.stopPropagation();
                      var data = params.row;
                      this.update(data);
                    }
                  }
                }, '修改')
              ]);
            }
          }
        ],
        data: [],
        dataDetail: {},
        initTable: [],
        format: Util.constant.format,
        addCurrentTableData: false,
        ajaxType: 'add',
        addData: {
          asset_number: '',
          type_id: '',
          remark: '',
          typeArr: [],
        },
        type_id: 'all',
        typeArr: [
          {
            id: 'all',
            name: '全部',
          },
        ],
        hold_id: '2',
        holdArr: [
          {
            id: '2',
            name: '全部',
          },
          {
            id: '0',
            name: '无人持有',
          },
          {
            id: '1',
            name: '有人持有',
          },
        ],
        selectionArr: [],
        approversArr: [],
        TreeData: [],
        checked_dep_arr: [],
        checked_users_arr: [],
      };
    },
    mounted () {
      this.getFixedAssetsTypeArr();
      this.getData();
    },
    methods: {
      onChangeTypeSelect: function (val) {
        var _this = this;
        _this.type_id = val;
        _this.page = 1;
        _this.getData();
      },
      onChangeHoldSelect: function (val) {
        var _this = this;
        _this.hold_id = val;
        _this.page = 1;
        _this.getData();
      },
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
          type_id: _this.type_id == 'all' ? '' : _this.type_id,
          hold: _this.hold_id,
        };
        var params = {
          url: '/asset/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            _this.data = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        //_this.show(index, data);
      },
      show: function (index, data) {
        var _this = this;
        var id = data.asset_type.id;
        _this.id = id;

        var data = {
          id: _this.id,
        };
        var params = {
          url: '/asset/repair/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.dataDetail = _data;
            _this.showCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 获取物品名
      getFixedAssetsTypeArr: function () {
        var _this = this;
        var data = {};
        var params = {
          url: '/asset/type/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.addData.typeArr = _data;
            var typeArr = _this.typeArr;
            for (var k in _data) {
              if (!_data[k]) {
                continue;
              }
              var id = _data[k].id;
              var title = _data[k].title;
              var obj = {
                id: id,
                name: title,
              };
              typeArr.push(obj);
            }
            _this.typeArr = typeArr;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        _this.addCurrentTableData = false;
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.addData.asset_number = '';
        _this.addData.remark = '';
        _this.addData.type_id = '';
        _this.ajaxType = 'add';
        _this.addCurrentTableData = true;
      },
      // 修改
      update: function (_data) {
        var _this = this;
        _this.id = _data.id;
        _this.addData.asset_number = _data.asset_number;
        _this.addData.remark = _data.remark;
        _this.addData.type_id = _data.type.id;
        _this.ajaxType = 'update';
        _this.addCurrentTableData = true;
      },
      // 保存数据
      save: function () {
        var _this = this;
        var asset_number = _this.addData.asset_number;
        var remark = _this.addData.remark;
        var type_id = _this.addData.type_id;
        if (type_id == '') {
          _this.$Message.error('请选择物品名!');
          return false;
        }
        if (asset_number == '') {
          _this.$Message.error('请填写编号!');
          return false;
        }
        var data = {
          id: _this.id,
          asset_number: asset_number,
          type_id: type_id,
          remark: remark,
        };
        var url = '/asset/add';
        if (_this.ajaxType == 'update') {
          url = '/asset/update';
        }
        var params = {
          url: url,
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            if (_this.ajaxType == 'add') {
              _this.page = 1;
            }
            _this.getData();
            _this.addCurrentTableData = false;
            _this.addData.asset_number = '';
            _this.addData.type_id = '';
            _this.addData.remark = '';
          } else {
            _this.$Message.error('保存信息失败!');
          }
        }
      },
      // 删除
      remove: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.error('请填选择要删除的数据!');
          return false;
        }
        var selectionIds = [];
        for (var k in selectionArr) {
          if (!selectionArr[k]) {
            continue;
          }
          var id = selectionArr[k].id;
          selectionIds.push(id);
        }
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            id: selectionIds,
          };
          var params = {
            url: '/asset/deleteMultiple',
            type: Util.method.get,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },

    }
  };
</script>
